require("../css/register.less")

window.onload = function () {
    // 定义一个全局变量,验证码
    let code = ""
    // 验证码
    let captcha1 = new CaptchaMini({
        lineWidth: 1, //线条宽度
        lineNum: 1, //线条数量
    });
    captcha1.draw(document.querySelector('#captcha1'), function (res) {
        console.log(res);
        code = res
    });

    // 验证
    let btn = document.querySelector(".btn")
    let telInp = document.querySelector("#tel")
    let yzmInp = document.querySelector("#yzmInp")
    let pwdInp = document.querySelector("#pwdInp")
    let repwdInp = document.querySelector("#repwdInp")
    btn.addEventListener("click", function () {
        let data = {
            account: telInp.value,
            password: pwdInp.value
        }
        // 非空验证
        if (!(telInp.value && yzmInp.value && pwdInp.value && repwdInp.value)) {
            tool.toast(0, "输入内容不能为空")
        } else {
            // 手机正则
            let reg = /^1[3-9][0-9]{9}$/
            if (!reg.test(telInp.value)) {
                tool.toast(0, "请输入正确的手机号格式")
            } else {
                // 验证码验证
                if (yzmInp.value.toLowerCase() != code.toLowerCase()) {
                    tool.toast(0, "请输入正确的验证码")
                } else {
                    // 密码一致性验证
                    if (pwdInp.value != repwdInp.value) {
                        tool.toast(0, "两次输入密码不一致")
                    } else {
                        // 发送ajax请求
                        http.post("/users/add", data, function (res) {
                            if (res.status === 0) {
                                tool.toast(1, "您已注册成功,正在跳转登录页面......")
                                location.href = "login.html"
                            } else {
                                tool.toast(0, "您的账号已注册")

                            }
                        })
                    }
                }
            }
        }
    })




}